<template>
	<u-sticky bg-color="#FFFFFF">
		<u-tabs :scrollable="false" lineColor="rgb(250,53,52)" :list="tabList" @click="tabClick" :current="tabCurrent" :activeStyle="{ color: 'rgb(250,53,52)', fontWeight: 'bold', transform: 'scale(1.05)' }" 
				:inactiveStyle="{ color: '#606266', transform: 'scale(1)' }"></u-tabs>
		<u-line></u-line>
	</u-sticky>
	<view>
		<u-list>
			<u-list-item class="bg-[#FFFFFF]" v-for="(item,index) in list" :key="index">
				<u-cell :title="item.Description" :label="item.AddDate">
					<template #value>
						<u-text :text="`${item.Amount}`" :color="item.Amount > 0 ? '#d81e06' : '#666666'" bold size="16"></u-text>
					</template>
				</u-cell>
			</u-list-item>
			<u-list-item>
				<u-loadmore line :status="isLoading ? 'loading' : hasMore ? 'loadmore' : 'nomore'"></u-loadmore>
			</u-list-item>
		</u-list>
	</view>
</template>

<script setup>
	
	import { nextTick, ref, unref } from "vue";
	import * as api from "@/api.js";
	import * as uniapp from "@dcloudio/uni-app";
	import {useUserStore} from "@/store/config.js";

	const store = useUserStore();
	const list = ref([]);
	const tabCurrent=ref(0);
	
	const tabList = ref([{
		name: '全部',
		value: ''
	}, {
		name: '消费积分',
		value: 1
	}, {
		name: '绿色积分',
		value: 2
	}, {
		name: '股权积分',
		value: 3
	}])
	
	const tabClick = e => {
		if(unref(tabCurrent) != e.index) {
			tabCurrent.value = e.index
			query.value.type = unref(tabList)[unref(tabCurrent)].value;
			query.value.page = 1;
			getData();
		}
	}
	
	const query = ref({
		type:'',
		page:1,
		pagesize:20
	})
	
	const isLoading = ref(false);
	const hasMore = ref(false);
	
	const getData = async () => {
		isLoading.value = true;
		const result = await api.getAmountList(store.user.UserID,unref(query));
		isLoading.value = false;
		await nextTick();
		if(result[1]) {
			uni.showToast({
				icon:'none',
				title:result[1]
			})
			query.value.page = Math.min(1,query.value.page - 1);
		} else {
			if(query.value.page == 1) {
				list.value = result[0].data
			} else {
				list.value = list.value.concat(result[0].data)
			}
			hasMore.value = result[0].pagecount > query.value.page
		}
	}
	
	uniapp.onLoad((option) => {
		if(option.type){
			tabCurrent.value=parseInt(option.type);
			query.value.type = unref(tabList)[unref(tabCurrent)].value;
			console.log(option.type);
		}
		getData()
	})
	
	uniapp.onPullDownRefresh(async() => {
		if(!unref(isLoading)) {
			query.value.page = 1;
			getData();
		} else {
			uni.showToast({
				icon:'none',
				title:'正在加载中，请稍后再试'
			})
			await uni.$u.sleep(200);
			uni.stopPullDownRefresh();
		}
	})
	
	uniapp.onReachBottom(() => {
		if(!unref(isLoading) && unref(hasMore)) {
			query.value.page = query.value.page + 1;
			getData();
		}
	})
	
</script>

<style>

</style>
